﻿using System;
using System.Data;
using l1cs.server.model.Instance;
using l1cs.server.serverpackets;
using l1cs.server.utils;

namespace l1cs.server.model.trap
{
    public abstract class L1Trap
    {
        protected readonly int _id;
        protected readonly int _gfxId;
        protected readonly bool _isDetectionable;

        public L1Trap( DataRow item )
        {
            _id = SqlUtils.toInt( item["id"] );
            _gfxId = SqlUtils.toInt( item["gfxId"] );
            _isDetectionable = Convert.ToBoolean( item["isDetectionable"] );
        }

        public L1Trap( int id, int gfxId, bool detectionable )
        {
            _id = id;
            _gfxId = gfxId;
            _isDetectionable = detectionable;
        }

        public int getId()
        {
            return _id;
        }

        public int getGfxId()
        {
            return _gfxId;
        }

        protected void sendEffect( L1Object trapObj )
        {
            if ( getGfxId() == 0 )
            {
                return;
            }
            S_EffectLocation effect = new S_EffectLocation( trapObj.getLocation(), getGfxId() );

            foreach ( L1PcInstance pc in L1World.getInstance().getRecognizePlayer( trapObj ) )
            {
                pc.sendPackets( effect );
            }
        }

        public abstract void onTrod( L1PcInstance trodFrom, L1Object trapObj );

        public void onDetection( L1PcInstance caster, L1Object trapObj )
        {
            if ( _isDetectionable )
            {
                sendEffect( trapObj );
            }
        }

        public static L1Trap newNull()
        {
            return new L1NullTrap();
        }
    }

    public class L1NullTrap : L1Trap
    {
        public L1NullTrap() : base( 0, 0, false ) { }

        override public void onTrod( L1PcInstance trodFrom, L1Object trapObj ) { }
    }
}
